package com.wjy.sfhcore.ui.widget;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.wjy.sfhcore.R;

import java.util.List;

/**
 * 图片浏览器
 *
 * @author wjy
 */
public class ImageBrowser extends RelativeLayout implements
        OnPageChangeListener {
    private MyViewPager pager;
    private LinearLayout viewGroup;
    private PointView[] points;
    private int index;
    private PagerAdapter mPagerAdapter;
    private List<String> imgs;

    public ImageBrowser(Context context) {
        super(context);
        init();
    }

    public ImageBrowser(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ImageBrowser(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.lib_view_image_browser, this);
        pager = (MyViewPager) findViewById(R.id.pager);
        viewGroup = (LinearLayout) findViewById(R.id.viewGroup);
    }

    public void setPagerIndex(int pagerIndex) {
        this.index = pagerIndex;
        pager.setCurrentItem(index);
    }

    public int getPagerIndex() {
        return index;
    }

    public void setPagerAdapter(PagerAdapter adapter, List<String> imgs) {
        this.mPagerAdapter = adapter;
        this.imgs = imgs;
        if (adapter != null && adapter.getCount() == 1) {
            viewGroup.setVisibility(View.INVISIBLE);
        }
        if (adapter != null && adapter.getCount() > 0) {
            viewGroup.removeAllViews();
            points = new PointView[imgs.size()];
            for (int i = 0; i < imgs.size(); i++) {
                PointView pointView = new PointView(getContext());
                // 将小圆点layout添加到数组中
                points[i] = pointView;

                // 默认选中的是第一张图片，此时第一个小圆点是选中状态，其他不是
                if (i == index) {
                    points[i].setSelected(true);
                } else {
                    points[i].setSelected(false);
                }

                // 将imageviews添加到小圆点视图组
                viewGroup.addView(points[i]);
            }
            pager.setAdapter(adapter);
            pager.setOnPageChangeListener(this);
            if (index > 0) {
                pager.setCurrentItem(index);
            }
        }
    }

    public void onPageScrollStateChanged(int arg0) {

    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    public void onPageSelected(int arg0) {
        index = arg0;
        for (int i = 0; i < imgs.size(); i++) {
            if (i == arg0 % imgs.size()) {
                points[i].setSelected(true);
            } else {
                points[i].setSelected(false);
            }
        }
    }

    public void setGravity(int gravity) {
        viewGroup.setGravity(gravity);
    }

    public ViewPager getViewPager() {
        return pager;
    }
}
